package com.eastedu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eastedu.repository.question.mapper.QuestionBasicInfoMapper;
import com.eastedu.repository.question.mapper.QuestionSourceMapper;
import com.eastedu.repository.question.mapper.Sheet1Mapper;
import com.eastedu.repository.question.po.QuestionBasicInfoPO;
import com.eastedu.repository.question.po.QuestionSourcePO;
import com.eastedu.repository.question.po.Sheet1PO;
import com.eastedu.service.AbstractDataSyncService;
import com.eastedu.service.DataSourceService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;


/**
 * 〈〉
 *
 * @author ZHISHUAI.ZHANG
 * @since 2021/7/28
 */
@Service
@DS("question")
public class QuestionProviderDataSourceServiceImpl extends AbstractDataSyncService<Sheet1PO> implements DataSourceService {

    private final QuestionSourceMapper questionSourceMapper;
    private final QuestionBasicInfoMapper questionBasicInfoMapper;

    public QuestionProviderDataSourceServiceImpl(Sheet1Mapper sheet1Mapper,
                                                 QuestionSourceMapper questionSourceMapper,
                                                 QuestionBasicInfoMapper questionBasicInfoMapper) {
        super(sheet1Mapper);
        this.questionSourceMapper = questionSourceMapper;
        this.questionBasicInfoMapper = questionBasicInfoMapper;
    }

    @Override
    protected void updateContent(Sheet1PO sheet1PO) {
        // 闻道资源校 需要根据学段更新
        String organization = "8c669c4b-a245-48da-ab03-4743f3c31838";
        QuestionBasicInfoPO questionBasicInfoPO = this.questionBasicInfoMapper.selectById(sheet1PO.getQuestionId());
        final List<QuestionSourcePO> questionSourcePoList = getQuestionSourceList(sheet1PO.getQuestionId());

        if (Objects.isNull(questionBasicInfoPO) || CollectionUtils.isEmpty(questionSourcePoList)) {
            return;
        }

        questionSourcePoList.forEach(o -> {
            if (Objects.nonNull(o.getProvider())) {
                return;
            }
            if (Objects.equals(o.getOrganization(), organization)) {
                if (Objects.equals(questionBasicInfoPO.getStudyPeriod(), sheet1PO.getStudyPeriod()) && Objects.equals(o.getOrganization(), sheet1PO.getOrganization())) {
                    updateProvider(sheet1PO, o);
                }
            } else {
                if (Objects.equals(o.getOrganization(), sheet1PO.getOrganization())) {
                    updateProvider(sheet1PO, o);
                }
            }
        });
        questionBasicInfoPO.setSources(JSONObject.toJSONString(questionSourcePoList));
        this.questionBasicInfoMapper.updateById(questionBasicInfoPO);
    }

    private void updateProvider(Sheet1PO sheet1PO, QuestionSourcePO o) {
        o.setProvider(sheet1PO.getUserId());
        o.setProviderName(sheet1PO.getUserName());
        this.questionSourceMapper.updateById(o);
    }

    private List<QuestionSourcePO> getQuestionSourceList(Long questionId) {
        return this.questionSourceMapper.selectList(Wrappers.<QuestionSourcePO>lambdaQuery()
                                                            .eq(QuestionSourcePO::getQuestionId, questionId));
    }
}